package tree.迭代bfs.剑指;

import po.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 剑指Offer从上到下打印二叉树32.java
 * @Description TODO
 * @createTime 2021年03月28日 11:04:00
 */
public class 剑指Offer从上到下打印二叉树32 {
    public int[] levelOrder(TreeNode root) {
        if (root==null){
            return new int[]{};
        }
        Queue<TreeNode> queue=new LinkedList<>();
        List<Integer> list=new ArrayList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode treeNode = queue.poll();
            list.add(treeNode.val);
            if (treeNode.left!=null){
                queue.offer(treeNode.left);
            }
            if (treeNode.right != null) {
                queue.offer(treeNode.right);
            }
        }
        int[] result=new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i]=list.get(i);
        }
        return result;
    }
}
